package au.edu.unsw.editingtool.web;

import org.apache.log4j.Logger;
import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.PatternLayout;

import fedora.client.FedoraClient;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;

import au.edu.unsw.editingtool.fedora.FedoraFactory;
import au.edu.unsw.editingtool.global.EditingToolException;

/**
 * Servlet implementation class Catch
 */
public class Catch extends HttpServlet {
	/**
	 * Logger for this class
	 */
	private static final Logger fedoraLogger = Logger.getLogger(FedoraClient.class);
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Catch() {
    	super();
    	String pattern = "Message: %m %n %n";
    	PatternLayout layout = new PatternLayout(pattern);
    	ConsoleAppender appender = new ConsoleAppender(layout);
    	fedoraLogger.addAppender(appender);
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		// TODO Auto-generated method stub
		//doTest(request, response);
		fedoraTest(request,response);		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		// TODO Auto-generated method stub
		//doTest(request, response);
		fedoraTest(request,response);
	}
	
	protected void doTest(HttpServletRequest req,
			HttpServletResponse res) throws ServletException, IOException {
		

	    res.setContentType("text/plain");
	    PrintWriter out = res.getWriter();

	    // Get the current session object, create one if necessary
	    HttpSession session = req.getSession();
	    out.println("in catch: session id is " + session.getId());
	    //session.invalidate();
	}
	
	protected void fedoraTest(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
		
		String url = "http://darkarrow.nun.unsw.edu.au:8080/fedora/services/management";
		url = "http://darkarrow.nun.unsw.edu.au:8080/fedora/";
		FedoraFactory fedora = new FedoraFactory(url, 
												 "fedoraAdmin",
												 "D4rk4rr0W");
		try {
			byte[] xml = fedora.getDatastream("dark:25", "MARCXML");
			res.getOutputStream().write(xml);
		} catch (EditingToolException e) {
			e.printStackTrace();
		} catch (TransformerConfigurationException e) {
			e.printStackTrace();
		} catch (TransformerException e) {
			e.printStackTrace();
		}
		
		res.flushBuffer();
		res.getOutputStream().close();
	}
	
	
}
